package com.xunyji.demo00testdemo.web.thymeleaf;

import com.xunyji.demo00testdemo.comm.util.weixin.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author AltEnter
 * @create 2019-01-12 15:10
 * @desc 微信登录控制类
 **/
@RequestMapping(value = "/wxLogin")
@Controller
@Slf4j
public class WeixinLoginController {

    @GetMapping
    public void toGetCode(HttpServletResponse response, ModelMap modelMap) throws IOException {
        // 回调地址：该地址必须能够在公网进行访问
        String backUrl = "http://xunyji.natapp1.cc/wxAuth/callback"; // natapp
//        String backUrl = "http://xunyuji.yicp.top/wxAuth/callback"; // 花生壳
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=APPID" +
                "&redirect_uri=REDIRECT_URI" +
                "&response_type=code" +
                "&scope=SCOPE" +
                "&state=STATE" +
                "#wechat_redirect";
        url = url.replace("APPID", AuthUtil.APPID)
                .replace("REDIRECT_URI", URLEncoder.encode(backUrl))
                .replace("SCOPE", "snsapi_userinfo");
        log.info("封装好的获取code的url为：" + url);

        response.sendRedirect(url);
//        modelMap.addAttribute("url", url);
//        return "index";
    }

}

